﻿using DbAccess;
using DbHelper.Demo.Entity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DbHelper.Demo.DAL
{
    public class UserDAL : Common
    {
        public UserDAL()
            : base("User")
        {
        }
        /// <summary>
        /// 数据读取器
        /// </summary>
        /// <param name="row">数据游标</param>
        /// <returns></returns>
        public static User DataReader(IDataReader row)
        {
            return DataReaderEx<User>(row);
        }

        /// <summary>
        /// 数据读取器
        /// </summary>
        /// <param name="row">数据游标</param>
        /// <typeparam name="T">数据类型</typeparam>
        /// <returns></returns>
        public static T DataReaderEx<T>(IDataReader row) where T : User, new()
        {
            T entity = new T
            {
                Id = row.GetInt32Ex(row.GetOrdinal("Id")),
                Name = row.GetStringEx(row.GetOrdinal("Name"))
            };
            return entity;
        }

        public User GetUserInfoWithId(int id)
        {
            string sql = $"SELECT * FROM {TableName} WHERE Id={id}";
            return QueryFirstOrDefault(sql, DataReader);
        }
        public List<User> GetListUser()
        {
            string sql = $"SELECT * FROM {TableName}";
            return Query(sql, DataReader);
        }
        public List<User> GetListPageUser(PageInfo pageInfo)
        {
            return QueryWithPage(TableName, pageInfo, DataReader);
        }
    }
}
